<?php
namespace app\web\controller;

use app\model\Theme; 
use app\model\Poster as Poster_mod;

class Poster extends Base
{
    public function Poster()
    {
global $_G, $_P;
$system = $_G['system'];
$_G['page']['title'] = '海报管理 - ' . $system['sysname'];
//load_func('tpl');
$GLOBALS['frames'] = array();
$themeid = intval($_P['themeid']);
if($themeid <= 0){
	$themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
	return_msg('还没有创建活动,请联系管理员');
}
$config = 	Theme::getConfig($themeid);
$do = 'poster';
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$status = intval($_P['status']);
$url = createWebUrl($do,array('op' => 'display','themeid'=>$themeid,'status'=>$status));

if($op == 'display') {

	//load_func('tpl');
	$item = Poster_mod::getByThemeIdnew($themeid,$status);
	if(!empty($item)) {
		$data = json_decode(str_replace('&quot;', '\'', $item['data']), true);
	}
	$themelist = Theme::getAllTheme();
	
	if(checksubmit('submit')) {
		$title = trim($_P['bg']);
		if(empty($title)){
			return_msg('背景图不可为空');
		}
		$data = array(
				'title'    => trim($_P['title']),
                'keyword'  => trim($_P['keyword']),
				'waittips' => trim($_P['waittips']),
				'bg'       => tomedia($_P['bg']),
				'data'     => htmlspecialchars_decode($_P['data']),
				'status'   => $status,
		);
		if($item['id'] >0) {
			db_update(t_poster, $data, array( 'id' => $item['id']));
		} else {
			$data['uniacid'] = $_G['mpid'];
            $data['themeid'] = $themeid;
			$insertid = db_insert(t_poster, $data);
			$posterid = $insertid;
		}
		if($status==0){
		    db_update(t_fans, array('poster' =>''), array('themeid' => $themeid));
        }else{
            db_update(t_worker, array('poster' =>''), array('themeid' => $themeid));
            //更新所有员工海报图
            $params = array();
            $params[':themeid'] = $themeid;
            $WorkerAll = db_select("SELECT * FROM ".tablename(t_worker)." WHERE  themeid = :themeid",$params);
            echo count($WorkerAll);
            if (!empty($WorkerAll)) {
                foreach ($WorkerAll as $key => $item) {
                    if (!empty($item['qrcode'])){
                        $qrimg = Poster_mod::base64_image_content($item['qrcode']);
                        //判断是否有设置首图海报，有则直接生成
                        $poster =  Poster_mod::getByThemeIdnew($themeid,1);
                        if(!empty($poster['id'])){
                            $member = array(
                                "openid"   =>$item['openid'],
                                'nickname' =>$item['nickname'],
                                'avatar'   =>$item['avatar'],
                                'themeid' =>$themeid,
                                'addtime' => microtime(),
                            );
                            $poster['bg']=str_replace("/99.jpg","/haibao.jpg",$poster['bg']);
                            $img = Poster_mod::createGoodsPoster($poster, $member, $qrimg);
                            $hbimg = str_replace("http://", "https://", $img['img']) ;
                            db_update(t_worker, array('poster' =>$hbimg,'qrcode'=> $qrimg), array('themeid' => $themeid, 'id' => $item['id']));
                        }
                    }
                }
            }
        }

		$data_old = array(
				'title'    => trim($item['title']),
                'keyword'  => trim($item['keyword']),
				'waittips' => trim($item['waittips']),
				'bg'       => trim($item['bg']),
				'data'     => $item['data'],
				'status'   => intval($item['status']),
		);
		writelog($themeid,'修改海报',$data_old,$data,$_G['username']);
        return_tips('编辑信息成功',$url, 'success');
	}
	
}

if($op == 'sheji'){
    db_delete ( t_poster, array ('themeid' => $themeid,'status'=>$status) );
    if($status==0){
        db_update(t_fans, array('poster' =>''), array('themeid' => $themeid));
    }else {
        db_update(t_worker, array('poster' => ''), array('themeid' => $themeid));
    }
	return_tips('清除成功',$url, 'success');
}
$return_clicent = get_defined_vars();
return view('/poster' , $return_clicent);
}
}